var entityCollection = require('../../../Base/server/controllers/EntityCollection');
var baseController = require('../../../Base/server/controllers/Base.server.controller');
var appEnum = require('../../../App/server/controllers/Enum.server.controller');
var fs = require('fs');
var publicConst = require("../../../Base/server/controllers/Const");

exports.getApps = function(req, res) {
    var parameter = req.body;
    //var enterpriseID = parameter.EnterpriseID;
    var entity = entityCollection.getEntity("App");

    entity.find(null, {
        Code: 1
    }, null, function(err, records) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            res.json({
                Data: records,
                Error: null
            });
        }
    });
};

//获取共享文件信息
exports.getSharedFileInfo = function(req, res) {
    var entity = entityCollection.getEntity("App");

    entity.find({
        IsEffective: true,
        Type: appEnum.DeviceType.MobilePlatform
    }, {
        Name: 1
    }, null, function(err, records) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            res.json({
                Data: records,
                Error: null
            });
        }
    });
};

//Get方式获取文件流
exports.getFileStreamByGet = function(req, res) {
    var fileName = req.params.FileName;
    var stream = fs.createReadStream(publicConst.FilePath + fileName);
    res.set('Content-Type', 'application/vnd.android.package-archive');
    stream.pipe(res);
};

//获取文件流
exports.getFileStream = function(req, res) {
    baseController.getFileStream(req, res);
};

//获取网关元数据信息
exports.getGateWayInfo = function(req, res) {
    var entity = entityCollection.getEntity("App");

    entity.find({
        IsEffective: true,
        Type: appEnum.DeviceType.Gateway
    }, {
        Name: 1
    }, null, function(err, records) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            if (records != null && records.length > 0) {
                res.json({
                    version: records[0].Version,
                    fileName: records[0].FileName
                });
            } else {
                res.json({
                    Data: records,
                    Error: null
                });
            }

        }
    });
};

//获取智能工厂元数据信息
exports.getRealTimeDCInfo = function(req, res) {
    var entity = entityCollection.getEntity("App");

    entity.find({
        IsEffective: true,
        Type: appEnum.DeviceType.SmartPlant
    }, {
        Name: 1
    }, null, function(err, records) {
        if (err) {
            res.json({
                Data: null,
                Error: err
            });
        } else {
            if (records != null && records.length > 0) {
                res.json({
                    version: records[0].Version,
                    fileName: records[0].FileName
                });
            } else {
                res.json({
                    Data: records,
                    Error: null
                });
            }

        }
    });
};